package com.gw.smscheck.controller.business.callrecords;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.gw.smscheck.entity.business.alarm.AlarmLog;
import com.gw.smscheck.entity.business.tools.Tools;
import com.gw.smscheck.entity.sys.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import com.gw.smscheck.util.ResultObj;
import com.gw.smscheck.util.ResultConversion;
import io.swagger.annotations.Api;
import springfox.documentation.annotations.ApiIgnore;
import io.swagger.annotations.ApiOperation;
import com.gw.smscheck.entity.business.callrecords.CallRecords;
import com.gw.smscheck.service.business.callrecords.CallRecordsService;
/**
 * CallRecords管理
 * 
 * @author pzr
 * @date 2018年10月29日
 */
@Api(description="【通话记录接口】")
@Controller
@RequestMapping("/callRecordsController")
public class CallRecordsController {
	@Autowired
	private CallRecordsService callRecordsService;
	/**
	 * 跳转到查询页面
	 * 
	 * @return
	 */
	@RequestMapping("/goPage")
	@ApiIgnore
	public String goPage() {
		return "business/views/url/callrecords";
	}
	/**
	 * 跳转到编辑页面
	 * 
	 * @return
	 */
	@RequestMapping("/goEdit")
	@ApiIgnore
	public ModelAndView goEdit( CallRecords callRecords) {
		String id = "";
		if (callRecords.getId() != null){
			id = callRecords.getId().toString();
		}
		ModelAndView modelAndView = new ModelAndView("business/views/url/callrecordsedit");
		modelAndView.addObject("id", id);
		return modelAndView;
	}
	/**
	 * 添加
	 * @param response
	 * @param callRecords
	 * @return
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="添加", notes="添加")
	public String add(HttpServletRequest request,HttpServletResponse response, CallRecords callRecords) {
		/* User user = (User) request.getSession().getAttribute("user");
		 callRecords.setCreateUser(String.valueOf(user.getId()));
		 System.out.println("打印登录人+++++++++++++"+user.getId());*/
		//生成uuid
        String uuid =getUUID();
        callRecords.setGroup(uuid);
        callRecordsService.add(callRecords);
        CallRecords records =new CallRecords();
        if(callRecords.getType()==1){
            records.setType(2);
        }else{
            records.setType(1);
        }
        records.setMyPhone(callRecords.getOtherPhone());
        records.setOtherPhone(callRecords.getMyPhone());
        records.setGroup(uuid);
		return ResultObj.successResultObj(callRecordsService.add(records));
	}
	/**
	 * 查询所有
	 * 
	 * @return
	 */
	@RequestMapping(value = "/findAll", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="查询所有", notes="查询所有")
	public String findAll() {
		List<CallRecords> list = callRecordsService.findAll();
		return JSON.toJSONString(list);
	}

    /**
     * 根据电话号码查询
     *
     * @return
     */
    @RequestMapping(value = "/findByPhone", method = { RequestMethod.POST})
    @ResponseBody
    @ApiOperation(value="根据电话号码查询", notes="根据电话号码查询")
    public String findByPhone(CallRecords callRecords) {
        List<CallRecords> list =callRecordsService.findByPhone(callRecords);
        return JSON.toJSONString(list);

    }
	/**
	 * 分页查询
	 * @param callRecords
	 * @return
	 */
	@RequestMapping(value = "/pageSelect", method = { RequestMethod.POST })
	@ResponseBody
	@ApiOperation(value="分页查询", notes="分页查询")
	public String pageSelect(CallRecords callRecords) {
		PageInfo<CallRecords> pageInfo = callRecordsService.pageSelect(callRecords);
		return ResultConversion.ToEasyUIDataGrid(pageInfo).toJSONString();
	}
	/**
	 * 通过id获取
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/findById", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="通过id获取", notes="通过id获取")
	public String findById(CallRecords dto){
		return ResultObj.successResultObj(callRecordsService.findById(dto.getId()));
	}
	/**
	 * 修改
	 * @param callRecords
	 * @return
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="修改", notes="修改")
	public String update(CallRecords callRecords) {
		return ResultObj.successResultObj(callRecordsService.update(callRecords));
	}
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/delByIds", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="批量删除", notes="批量删除")
	public String delByIds(String[] ids){
		System.out.println(ids.toString());
		return ResultObj.successResultObj(callRecordsService.delByIds(ids));
	}
    /**
     * 获得一个uuid
     * @param
     * @return
     */
    public String getUUID(){
        String uuid = UUID.randomUUID().toString();
       //去掉“-”符号
        return uuid.replaceAll("-", "");
    }

	/**
	 *通话记录查询
	 *
	 * @return
	 */
	@RequestMapping(value = "fingCallRecords", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="通话记录查询", notes="通话记录查询")
	public String fingCallRecords(CallRecords callRecords) {
        System.out.println("打印参数============"+callRecords.getNumbering());
        Integer days = callRecords.getDayNum();
		System.out.println("打印参数============"+days);
		if( callRecords.getDayNum()==null||callRecords.getDayNum()==0){
			System.out.println("进来+++++++++++");
			days=3;
		}
		long  nowtime= System.currentTimeMillis();
		long  nowtime1 = nowtime-(days-1)*24*60*60*1000;
		SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd ");
		String retStrFormatNowDate = sdFormatter.format(nowtime1);
		String retStrFormatNowDate1 = sdFormatter.format(nowtime);
		String startTime = retStrFormatNowDate+"00:00:00";
		String endTime = retStrFormatNowDate1+"23:59:59";
		System.out.println("打印时间"+startTime);
		System.out.println("打印时间"+endTime);
		callRecords.setStartTime(startTime);
		callRecords.setEndTime(endTime);
		List<CallRecords> list =callRecordsService.fingCallRecordsOut(callRecords);
		List<CallRecords> list1 =callRecordsService.fingCallRecordsIn(callRecords);
		Map<String, List> map = new HashMap<String, List>();
		map.put("out",list);
		map.put("in",list1);
		return ResultObj.successResultObj(map);
	}

}

